package com.huayue.controller;

import java.math.BigDecimal;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.entity.Supplier;
import com.huayue.service.SupplierService;
import com.huayue.util.DateHelper;
import com.huayue.util.PageHelper;

/**
 * 供应商欠款
 */
@Controller
@RequestMapping("/supplierManage")
public class SupplierManagerController {
    @Autowired
    SupplierService svc;
    BaseController bc = new BaseController();

    @RequestMapping("/find")
    public ModelAndView find(PageHelper pages, String dateRange, Supplier supplier) {
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }

        List<Map<String, Object>> list = svc.findAll();
        BigDecimal totalprice = new BigDecimal(0);
        BigDecimal total = new BigDecimal(0);
        List<Map<String, Object>> list2 = new ArrayList<>();
        FastJsonJsonView view = new FastJsonJsonView();
        for (Map<String, Object> map : list) {
            String id = map.get("id").toString();
            String name = map.get("name").toString();

            List<Map<String, Object>> list1 = svc.findSupplier(id, begin, end);

            List<Map<String, Object>> list3 = svc.find(id, begin, end);
            for (Map<String, Object> map1 : list1) {
                BigDecimal price = new BigDecimal(map1.get("price").toString());
                totalprice = totalprice.add(price);
            }

            for (Map<String, Object> map1 : list3) {
                BigDecimal price = new BigDecimal(map1.get("price").toString());
                total = total.add(price);
            }

            Map<String, Object> map1 = new HashMap<>();
            map1.put("id", id);
            map1.put("name", name);
            map1.put("totalprice", totalprice);
            map1.put("price", total);
            list2.add(map1);
            totalprice = new BigDecimal(0);
            total = new BigDecimal(0);
        }

        // view.addStaticAttribute("findpage",bc.findPage(ssvc,page,supplier,supplier));
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("message", "查询成功");
        view.addStaticAttribute("count", pages.getCount());
        view.addStaticAttribute("data", list2);

        // System.out.println(list2);

        return new ModelAndView(view);
    }

    @RequestMapping("/update")
    public ModelAndView update(String id, String dateRange) {
        String begin = null;
        String end = null;
        if (dateRange != null && !"".equals(dateRange)) {
            begin = dateRange.split(" - ")[0];
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(DateHelper.StringToDateShort(dateRange.split(" - ")[1]));
            calendar.add(Calendar.DATE, 1);
            end = DateHelper.DateToStringShort(calendar.getTime());
        }
        FastJsonJsonView view = new FastJsonJsonView();
        int status = svc.update(id, begin, end);
        view.addStaticAttribute("code", 0);
        if (status > 0) {
            view.addStaticAttribute("status", status);
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "修改成功");
        } else {
            view.addStaticAttribute("status", status);
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "修改失败");
        }

        return new ModelAndView(view);

    }

}
